package com.mantru.iotpot.Adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import com.mantru.iotpot.Adapter.Base.AppAdapter
import com.mantru.iotpot.Adapter.Base.AppHolder
import com.mantru.iotpot.Bean.DishesListShowBean
import com.mantru.iotpot.R

class DishesShowAdapter(var context: Context, layoutResId: Int, var data: MutableList<DishesListShowBean>): AppAdapter<DishesListShowBean>(context, layoutResId, data) {
    private var dishesShowListener: DishesShowListener? = null
    fun setDishesShowListener(dishesShowListener: DishesShowListener?) {
        this.dishesShowListener = dishesShowListener
    }
    interface DishesShowListener {
        fun onClick(position: Int)
    }

    override fun convert(holder: AppHolder?, position: Int, item: DishesListShowBean) {
        holder?.setText(R.id.adapter_dishes_name, item.name)
            ?.setBackground(R.id.adapter_dishes_background, item.image)
            ?.setOnClickListener(R.id.adapter_dishes){
                dishesShowListener!!.onClick(position)
            }
    }

    @SuppressLint("NotifyDataSetChanged")
    //刷新数据
    fun updateData(newData: List<DishesListShowBean>) {
        (context as? Activity)?.runOnUiThread {
            (mItems as? MutableList)?.apply {
                clear()
                addAll(newData)
            }
            notifyDataSetChanged()
        }
    }
}